package com.example.smarthome.ui.home;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smarthome.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AirCollect extends AppCompatActivity {

    String model="";
    String wendu="";
    private static final String DeviceID2="1064814621";
    private static final String ApiKey ="1SM9vECD95xFxkRzxivzktUNQko=";
    TextView data1,data2,data3;
    String value1,value2,value3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_air_collect);
        RadioButton radioButton =(RadioButton) findViewById(R.id.cold) ;
        RadioButton radioButton1=(RadioButton) findViewById(R.id.hot) ;
        EditText editText=(EditText) findViewById(R.id.Edix_text_wendu);
        Button button1 = (Button)findViewById(R.id.confirm);
        Button button2= (Button) findViewById(R.id.add);
        Button button3= (Button) findViewById(R.id.reduce);
        wendu= editText.getText().toString();
        model=radioButton.getText().toString();
//        button1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.e("输入的温度当前", wendu);
//                Log.e("输入的温度当前", model);
//                if(model=="制冷")
//                {
//                    Toast.makeText(AirCollect.this, "hhah", Toast.LENGTH_SHORT).show();
//                    lightControl("D");
//
//                }
//                if(model=="制热")
//                    lightControl("C");
//
//            }
//        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text= editText.getText().toString();
                int i=Integer.valueOf(text).intValue()+1;
                wendu = Integer.toString(i);
                editText.setText(wendu);
                Log.e("输入的温度当前", text);

                    lightControl("E");
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text= editText.getText().toString();
                int i=Integer.valueOf(text).intValue()-1;
                wendu = Integer.toString(i);
                editText.setText(wendu);
                Log.e("输入的温度当前", wendu);
                    lightControl("F");
            }

        });
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text=radioButton.getText().toString();
                model=text;
                Log.e("输入的模式当前", text);
                lightControl("D");
            }
        });
        radioButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text=radioButton1.getText().toString();
                model=text;
                Log.e("输入的模式当前", text);
                lightControl("C");
            }
        });
    }
    public void lightControl( String index) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String cmd=index;
                String body=String.format(cmd);
                String url=String.format("http://api.heclouds.com/cmds?device_id="+DeviceID2);
                OkHttpClient client=new OkHttpClient();
                RequestBody requestBody =RequestBody.create(MediaType.parse("application/json;charset=utf-8"),body);
                Request request=new Request.Builder().url(url).header("api-key",ApiKey).post(requestBody).build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        String insertStr="请求失败";
                        Log.i("请求失败","22");
//                        Toast.makeText(MasterbedroomFragment.this,insertStr, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                    }
                });
            }
        }).start();
    }
}